package com.asm.service;

import com.asm.dto.PageResult;
import com.asm.entity.sys.Community;
import com.asm.repository.CommunityRepository;
import com.asm.util.SnowflakeIdWorker;
import com.asm.vo.sys.CommunityVo;
import com.sun.javafx.scene.web.Debugger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.geo.Point;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.*;

/**
 * @author morningbear
 */
@Service
public class CommunityService {
    @Autowired
    private CommunityRepository communityRepository;

    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;


    private Map<String, Object> getCommunityResult(Community community){
        Map<String, Object> result = new HashMap<>();
        result.put("id", community.getId());
        result.put("community_type", community.getType());
        result.put("community_longitude", community.getLongitude());
        result.put("community_latitude", community.getLatitude());
        result.put("district_id", community.getDistrictId());
        result.put("trade_area_id", community.getTradeAreaId());
        return result;
    }

    private PageResult<CommunityVo> getCommunityVoPageResult(Integer pageSize, Integer pageNumber, Page<Community> page) {
        List<CommunityVo> communityList = new ArrayList<>();
        for (Community community : page.getContent()) {
            CommunityVo communityVo = new CommunityVo();
            BeanUtils.copyProperties(community, communityVo);
            communityList.add(communityVo);
        }

        return new PageResult<CommunityVo>(pageNumber, pageSize, page.getTotalPages(), communityList);
    }

    /**
     * 查询所有小区
     * @return PageResult<CommunityVo>
     */
    public PageResult<CommunityVo> findAllCommunities(Integer pageSize, Integer pageNumber){
        Page<Community> page = communityRepository.findAll(PageRequest.of(pageNumber, pageSize));
        return getCommunityVoPageResult(pageSize, pageNumber, page);
    }

    /**
     *根据小区类型查找小区
     * @param type 小区类型
     * @return PageResult<CommunityVo>
     */
    public PageResult<CommunityVo> findByCommunityType(String type, Integer pageNumber, Integer pageSize){
        Page<Community> page = communityRepository.findByType(type, PageRequest.of(pageNumber, pageSize));
        return getCommunityVoPageResult(pageSize, pageNumber, page);
    }

    /**
     *根据小区名字查找小区
     * @param name String
     * @return PageResult<CommunityVo>
     */
    public PageResult<CommunityVo> findByCommunityName(String name, Integer pageNumber, Integer pageSize){
        Page<Community> page = communityRepository.findByName(name, PageRequest.of(pageNumber, pageSize));
        return getCommunityVoPageResult(pageSize, pageNumber, page);
    }

    /**
     *根据小区地址查找小区
     * @param address String
     * @return List<Map<String, Object>>
     */
//    public List<Map<String, Object>> findByCommunityAddress(Point address){
//        List<Community> communityList = communityRepository.findByAddress(address);
//        List<Map<String, Object>> results = new ArrayList<>();
//        for (Community community : communityList){
//            results.add(getCommunityResult(community));
//        }
//        return results;
//    }

    /**
     *根据镇/区编号查找小区
     * @param districtId Long
     * @return PageResult<CommunityVo>
     */
    public PageResult<CommunityVo> findByCommunityDistrictId(Long districtId, Integer pageNumber, Integer pageSize){
        Page<Community> page = communityRepository.findByDistrictId(districtId, PageRequest.of(pageNumber, pageSize));
        return getCommunityVoPageResult(pageSize, pageNumber, page);
    }

    /**
     *根据商圈编号查找小区
     * @param tradeAreaId Long
     * @return PageResult<CommunityVo>
     */
    public PageResult<CommunityVo> findByCommunityTradeAreaId(Long tradeAreaId, Integer pageNumber, Integer pageSize){
        Page<Community> page = communityRepository.findByTradeAreaId(tradeAreaId, PageRequest.of(pageNumber, pageSize));
        return getCommunityVoPageResult(pageSize, pageNumber, page);
    }

    /**
     * 根据编号查询小区
     * @param id 小区编号
     * @return  Map<String, Object>
     */
    public Map<String, Object> getCommunity(Long id){
        return getCommunityResult(communityRepository.getOne(id));
    }

    /**
     * 添加小区
     * @param communityVo 小区
     * @return String
     */
    @Transactional(rollbackFor = Exception.class)
    public void addCommunity(CommunityVo communityVo){
        Community community = new Community();
        BeanUtils.copyProperties(communityVo, community);
        community.setId(snowflakeIdWorker.nextId());
        community.setCreateBy(1L);
        community.setCreateAt(new Timestamp(System.currentTimeMillis()));
        community.setUpdateBy(1L);
        community.setUpdateAt(new Timestamp(System.currentTimeMillis()));
        communityRepository.save(community);
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteCommunity(Long id){
        communityRepository.deleteById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateCommunity(CommunityVo communityVo){
        Community community = communityRepository.findById(communityVo.getId()).get();
        BeanUtils.copyProperties(communityVo, community);
        community.setUpdateBy(1L);
        community.setUpdateAt(new Timestamp(System.currentTimeMillis()));
        communityRepository.save(community);
    }
}
